//
//  CZMainTabBar.swift
//  weibo10
//
//  Created by apple on 16/5/11.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class CZMainTabBar: UITabBar {

   //加号按钮调用闭包
    var composeCallback: (() -> ())?
    
    
    
    
  //布局按钮
    override func layoutSubviews() {
        
        
        //计算frame的宽度
        let width = self.frame.width / 5
        
        //记录按钮的位置
        var index = 0
        
        for view in self.subviews
        {
        if view.isKindOfClass(NSClassFromString("UITabBarButton")!)
        {
           //重置frame
            view.frame = CGRect(x: CGFloat(index) * width, y: 0, width: width, height: self.frame.height)
            
            index++
            
            if index == 2
            {
            index++
            
            }
            }
        }
        
        //设置加号按钮的位置
        composeButton.frame = CGRect(x: 2 * width, y: 0, width: width, height: self.frame.height)
        
    }
    
  //按钮点击事件 
    func composeButtonClick(button:UIButton)
    {
    //调用闭包
        composeCallback?()
    
    }
    
    
  
    
    //懒加载
 lazy  var composeButton:UIButton =
    {
        //创建按钮
      
        let button = UIButton()
       
       //设置按钮背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        //高亮背景图
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        //设置按钮图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        //高亮图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        //将按钮添加到view
        self.addSubview(button)
        
        
        
        
        
        
       return button
    }()
    
    

}

